// 封装购物车模块

import { defineStore } from 'pinia'
import { ref } from 'vue'
import { addCartApi } from '@/api/cart.js'
import { getShoppingCartApi } from '@/api/goods.js'
import { useUserStore } from '@/stores/userStore.js'

export const useCartStore = defineStore('cart', () => {
  // 1.定义state--cartList
  const cartList = ref([])
  const userStore = useUserStore()
  const getCartList = async () => {
    if (!userStore.user) {
      return
    }
    const res = await getShoppingCartApi(userStore.user.uid)
    cartList.value = res.data
  }
  // 2.定义action--addCart
  const addCart = async (goods) => {
    // 添加购物车操作
    await addCartApi(goods)
  }
  return {
    cartList,
    addCart,
    getCartList
  }
})